package com.adgox.tiantianbiting.mine.message;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.ActivityAdapter;
import com.adgox.tiantianbiting.adapter.CategoryAdapter;
import com.adgox.tiantianbiting.adapter.MessageAdapter;
import com.adgox.tiantianbiting.bean.MessageBean;
import com.adgox.tiantianbiting.bean.MessageCountBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.home.category.CategoryActivity;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.main.MainActivity;
import com.adgox.tiantianbiting.mine.tingCoinRecord.TingCoinRecordActivity;
import com.adgox.tiantianbiting.mine.wallet.MyWalletActivity;
import com.adgox.tiantianbiting.redpacket.sign.SignActivity;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.adgox.tiantianbiting.utils.SpUtils;
import com.adgox.tiantianbiting.utils.WebviewActivity;
import com.adgox.tiantianbiting.widget.WaveAnimationView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MessageActivity extends AppCompatActivity implements MessageContract.View {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.finish)
    ImageView finish;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.system_unread_count)
    TextView system_unread_count;
    @BindView(R.id.activity_unread_count)
    TextView activity_unread_count;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.view5)
    View view5;

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.swiperefresh1)
    SwipeRefreshLayout swiperefresh1;
    @BindView(R.id.swiperefresh2)
    SwipeRefreshLayout swiperefresh2;
    @BindView(R.id.rv2)
    RecyclerView rv2;
    //    @BindView(R.id.waveview)
//    WaveAnimationView waveview;
    private int currentSystemPage = 1;
    private int currentActivityPage = 1;
    private int maxPage = 10;
    private int totalSystemPages = 1;
    private int totalActivityPages = 1;
    private MessageAdapter messageAdapter;
    private ActivityAdapter activityAdapter;
    private List<MessageBean.DataBeanX.DataBean> programList = new ArrayList<>();
    private List<MessageBean.DataBeanX.DataBean> activityList = new ArrayList<>();

    private MessageContract.Presenter mPresenter;
    private Intent intent;
    private static final String SYSTEM_MSG = "systemmsg";
    private static final String ACTIVITY_MSG = "activitymsg";
    private static final String MSG_COUNT = "MSG_COUNT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        title.setText("消息中心");
        SpUtils.setString(this, MSG_COUNT, String.valueOf(new Date().getTime()));

        new MessagePresenterImpl(this);
        mPresenter.start();
        mPresenter.getSystem(1, 10, 0);
        if (TextUtils.isEmpty(SpUtils.getString(this, SYSTEM_MSG))) {
            mPresenter.getSystemCount(0, 0);
        } else {
            mPresenter.getSystemCount(Long.parseLong(SpUtils.getString(this, SYSTEM_MSG)), 0);

        }


        if (TextUtils.isEmpty(SpUtils.getString(this, ACTIVITY_MSG))) {

            mPresenter.getSystemCount(0, 1);
        } else {
            mPresenter.getSystemCount(Long.parseLong(SpUtils.getString(this, ACTIVITY_MSG)), 1);
        }
        initAdapter();
//        waveview.startAnimation();
    }

    //
//    @Override
//    protected void onPause() {
//        super.onPause();
//        waveview.stopAnimation();
//    }
//
//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        waveview.startAnimation();
//    }
    private void initAdapter() {
        //系统消息adapter
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);
        swiperefresh1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentSystemPage = 1;
                mPresenter.getSystem(currentSystemPage, maxPage, 0);
            }
        });
        messageAdapter = new MessageAdapter(new ArrayList<MessageBean.DataBeanX.DataBean>());
        messageAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (currentSystemPage < totalSystemPages) {
                    currentSystemPage++;
                    mPresenter.getSystem(currentSystemPage, maxPage, 0);
                } else {
                    messageAdapter.loadMoreEnd();
                }
            }
        }, rv);
        messageAdapter.setEmptyView(View.inflate(this, R.layout.empty_message, null));
        rv.setAdapter(messageAdapter);
        rv.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageBean.DataBeanX.DataBean bean = (MessageBean.DataBeanX.DataBean) adapter.getData().get(position);
                switch (bean.getSysMsgType()) {
                    //        sysMsgType (integer, optional): 系统消息类型 0.奖励听币通知 1.奖励可提现通知 2.签到 3.欢迎礼包已发送 4.栏目降价 5.栏目购买成功 6.充值 7.栏目付费提醒 ,
                    case 0:
                        intent = new Intent(MessageActivity.this, TingCoinRecordActivity.class);
                        break;
                    case 1:
//                        奖励可提现通知
                        intent = new Intent(MessageActivity.this, TingCoinRecordActivity.class);
                        break;
                    case 2:
//                        签到
                        intent = new Intent(MessageActivity.this, SignActivity.class);
                        break;
                    case 3:
//                        欢迎礼包已发送,暂不做
                        intent = new Intent(MessageActivity.this, MainActivity.class);
                        break;
                    case 4:
//                        栏目降价,暂不做
                        intent = new Intent(MessageActivity.this, MainActivity.class);
                        break;
                    case 5:
                        intent = new Intent(MessageActivity.this, ProgramDetailActivity.class);
                        intent.putExtra("programId", Integer.valueOf(bean.getParam()));
//                        intent.putExtra("programName", "别闹");
                        break;
                    case 6:
                        intent = new Intent(MessageActivity.this, MyWalletActivity.class);
                        break;
                    case 7:
//                        栏目付费提醒,暂不做
                        intent = new Intent(MessageActivity.this, MainActivity.class);
                        break;
                }
                startActivity(intent);
            }
        });
        //活动消息adapter
        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        rv2.setLayoutManager(manager2);
        activityAdapter = new ActivityAdapter(new ArrayList<MessageBean.DataBeanX.DataBean>());
        activityAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (currentActivityPage < totalActivityPages) {
                    currentActivityPage++;
                    mPresenter.getActivity(currentActivityPage, maxPage, 1);
                } else {
                    activityAdapter.loadMoreEnd();
                }
            }
        }, rv2);
        rv2.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageBean.DataBeanX.DataBean data = (MessageBean.DataBeanX.DataBean) adapter.getData().get(position);
                Intent intent = new Intent(MessageActivity.this, WebviewActivity.class);
                intent.putExtra("url", data.getLinkUrl());
                intent.putExtra("title", data.getTitle());
                startActivity(intent);
            }
        });
        activityAdapter.setEmptyView(View.inflate(this, R.layout.empty_message, null));
        swiperefresh2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentActivityPage = 1;
                mPresenter.getActivity(currentActivityPage, maxPage, 1);
            }
        });
        rv2.setAdapter(activityAdapter);

    }

    private void showLoading() {
        DialogUtils.showLoadingDialog(this);
    }

    @OnClick({R.id.finish, R.id.textView3, R.id.textView4, R.id.textView5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.textView3:
                swiperefresh1.setRefreshing(true);
                setBold(textView5, false);
                setBold(textView3, true);
                setBold(textView4, false);
                setVisible(view3, true);
                setVisible(view4, false);
                setVisible(view5, false);
                setVisible(swiperefresh1, true);
                setVisible(swiperefresh2, false);
                currentSystemPage=1;
                mPresenter.getSystem(1, 10, 0);
                break;
            case R.id.textView4:
                swiperefresh2.setRefreshing(true);
                setBold(textView5, false);
                setBold(textView3, false);
                setBold(textView4, true);
                setVisible(view3, false);
                setVisible(view4, true);
                setVisible(view5, false);
                setVisible(swiperefresh1, false);
                setVisible(swiperefresh2, true);
                currentActivityPage=1;
                mPresenter.getActivity(1, 10, 1);
                break;
            case R.id.textView5:
                setBold(textView5, true);
                setBold(textView3, false);
                setBold(textView4, false);
                setVisible(view3, false);
                setVisible(view4, false);
                setVisible(view5, true);
                setVisible(swiperefresh1, false);
                setVisible(swiperefresh2, false);
                mPresenter.getComment(1, 10, 2);
                Toast.makeText(MessageActivity.this, "暂未开放", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public void setBold(TextView view, boolean isTrue) {
        if (isTrue) {
            view.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            view.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        }
    }

    public void setVisible(View view, boolean isTrue) {
        if (isTrue) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    @Override
    public void setSystem(MessageBean.DataBeanX beanX) {
        long time = new Date().getTime();
        SpUtils.setString(this, SYSTEM_MSG, String.valueOf(time));
        swiperefresh1.setRefreshing(false);
        if ((beanX == null || beanX.getData() == null) || beanX.getData().size() == 0) {
            Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
            return;
        }
        totalSystemPages = beanX.getTotalPages();
        if (totalSystemPages == 1) {
            messageAdapter.loadMoreEnd();
        }

        if (currentSystemPage == 1) {
            messageAdapter.setNewData(beanX.getData());
            messageAdapter.loadMoreComplete();
        } else {
            messageAdapter.addData(beanX.getData());
            if (beanX.getData().size()<10){
                messageAdapter.loadMoreEnd();
            }

        }
    }

    @Override
    public void setSystemCount(MessageCountBean.DataBean beanX) {
        system_unread_count.setVisibility(beanX.getSysMsg() == 0 ? View.GONE : View.VISIBLE);
        if (beanX.getSysMsg() < 10) {
            system_unread_count.setBackgroundResource(R.drawable.shape_message_one);
            system_unread_count.setText(beanX.getSysMsg() + "");

        } else if (beanX.getSysMsg() <= 99) {
            system_unread_count.setBackgroundResource(R.drawable.shape_message_ten);
            system_unread_count.setText(beanX.getSysMsg() + "");

        } else {
            system_unread_count.setBackgroundResource(R.drawable.shape_message_ten);
            system_unread_count.setText("99+");
        }
    }

    @Override
    public void setActivityCount(MessageCountBean.DataBean bean) {
        activity_unread_count.setVisibility(bean.getSysMsg() == 0 ? View.GONE : View.VISIBLE);
        if (bean.getSysMsg() < 10) {
            activity_unread_count.setBackgroundResource(R.drawable.shape_message_one);
            activity_unread_count.setText(bean.getSysMsg() + "");
        } else if (bean.getSysMsg() <= 99) {
            activity_unread_count.setBackgroundResource(R.drawable.shape_message_ten);
            activity_unread_count.setText(bean.getSysMsg() + "");
        } else {
            activity_unread_count.setBackgroundResource(R.drawable.shape_message_ten);
            activity_unread_count.setText("99+");
        }
    }

    @Override
    public void setActivity(MessageBean.DataBeanX beanX) {
        long time = new Date().getTime();
        SpUtils.setString(this, ACTIVITY_MSG, String.valueOf(time));
        swiperefresh2.setRefreshing(false);
        if ((beanX == null || beanX.getData() == null) || beanX.getData().size() == 0) {
            Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
            return;
        }
        totalActivityPages = beanX.getTotalPages();
        if (totalActivityPages == 1) {
            activityAdapter.loadMoreEnd();
        }

        if (currentActivityPage == 1) {
            activityAdapter.setNewData(beanX.getData());
            activityAdapter.loadMoreComplete();
        } else {
            activityAdapter.addData(beanX.getData());

            if (beanX.getData().size()<10){
                activityAdapter.loadMoreEnd();
            }
        }

    }

    @Override
    public void setComment(MessageBean.DataBeanX beanX) {

    }

    @Override
    public void onGetDataFailed(String msg) {
        swiperefresh1.setRefreshing(false);
        long time = new Date().getTime();
        SpUtils.setString(this, SYSTEM_MSG, String.valueOf(time));

    }

    @Override
    public void onGetDataFailed2(String msg) {
        swiperefresh2.setRefreshing(false);
        long time = new Date().getTime();
        SpUtils.setString(this, ACTIVITY_MSG, String.valueOf(time));
    }

    @Override
    public void setPresenter(MessageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }
}
